package com.liliudong.pinkyponk.biz.base;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.liliudong.pinkyponk.core.util.R;
import org.springframework.http.ResponseEntity;

import java.util.Collection;
import java.util.List;

/**
 * BaseController
 *
 * @author liliudong
 * @date 2022-06-01 16:59
 **/
public abstract class BaseController<Entity, Req, Res, Convert extends BaseConvert<Entity, Req, Res>, Service extends IService<Entity>> {

    protected final Convert convert;
    protected final Service service;

    public BaseController(Convert convert, Service service) {
        this.convert = convert;
        this.service = service;
    }

    protected ResponseEntity<?> ok(Entity entity) {
        return R.ok(entityToRes(entity));
    }

    protected ResponseEntity<?> ok(Collection<Entity> entities) {
        return R.ok(entityListToResList(entities));
    }

    protected ResponseEntity<?> ok(Page<Entity> page) {
        return R.ok(page);
    }

    protected ResponseEntity<?> ok() {
        return R.ok();
    }

    protected Res entityToRes(Entity entity) {
        return convert.entityToRes(entity);
    }

    protected Entity reqToEntity(Req req) {
        return convert.reqToEntity(req);
    }

    protected List<Res> entityListToResList(Collection<Entity> entities) {
        return convert.entityListToResList(entities);
    }

    protected Page<Entity> getPageQuery(Long current) {
        return new Page<>();
    }

    protected Page<Entity> getPageQuery(Long current, Long size) {
        return new Page<>(current, size);
    }
}
